package com.atguigu.springcloud.controller;


import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import com.atguigu.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.javassist.bytecode.stackmap.BasicBlock;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.web.bind.annotation.*;
import org.springframework.cloud.client.discovery.DiscoveryClient;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

//controller  掉service   service 掉dao
@RestController
@Slf4j
@RequestMapping("Payment")
public class PaymentController {
    //获取yml中的端口号
    @Value("${server.port}")
    private String serverPort;
    //服务发现  注意到包是spring的包
    @Resource
    private DiscoveryClient discoveryClient;
    //调用service完成逻辑
    @Resource
    private PaymentService paymentService;

    @PostMapping("/create")
    public CommonResult create(@RequestBody Payment payment) {
        int result = paymentService.create(payment);
        log.info("******插入结果" + result);
        if (result > 0) {
            return new CommonResult(200, "插入数据库成功,serverPort" + serverPort, result);
        } else {
            return new CommonResult(500, "插入数据库失败", null);
        }
    }

    //http://localhost:8001/Payment/getPaymentById/1
    @GetMapping("/getPaymentById/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        Payment paymentById = paymentService.getPaymentById(id);
        log.info("******插入结果" + paymentById + "测试热部署");
        if (paymentById != null) {
            return new CommonResult(200, "查询据库成功,serverPort" + serverPort, paymentById);
        } else {
            return new CommonResult(500, "查询数据库失败，没有对应id" + id, null);
        }

    }

    @GetMapping("/discovery")
    public Object discovery() {
        //记录eureka中登录好的微服务
        List<String> services = discoveryClient.getServices();
        //iter快捷键for
        for (String element : services) {
            log.info("***********element:" + element);
        }
        //获取实例  我们对外暴露的微服务名称的id（通过微服务名称，进一步获取他们的端口号（具体的id））
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        for (ServiceInstance instance : instances) {
            //名称  地址 端口号
            log.info("**********:" + instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getUri());
        }
        //返回当前注册的信息
        return discoveryClient;
    }

    @GetMapping(value = "/lb")
    public String getPaymentLB() {
        return serverPort;
    }

    /**
     * cloud-consumer-feign-order80
     * <p>
     * 模拟服务端所用的时长超过客户端的等待
     *
     * @return
     */
    @GetMapping("/feign/timeout")
    public String PaymentFeignTimeOut() {
        //单位  为秒
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return  serverPort;
    }

}
